var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    finishFilesList: [],
    issuccessMask: false,
    buildId: '',
    projectDescribe: '',
    ypage:false,
   
  },

  godetail() {
    wx.navigateTo({
      url: '/pages/projectDetails/projectDetails?orderId=' + this.data.projectdestil.id,
    })
  },


  // 项目详情接口
  getOrderDetails() {
    let t = this
    wx.showLoading({
      title: '加载中。。。', //提示的内容
      mask: true //显示透明蒙层 防止触摸穿透
    })
    wx.request({
      url: app.globalData.Interfaceurl + 'getOrderDetails',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
        userId: app.globalData.userId,
        // userId: 1569562832576524288,
        orderId: t.data.projectdestil.id,
      },
      success: res => {
      if(res.data.buildProofFlag==1){
        let finishFilesList = res.data.buildList[0].buildAtlas.split(",").map(item => app.globalData.Imageurl + item)

        t.setData({
          finishFilesList: finishFilesList,
          buildId: res.data.buildList[0].id,
          projectDescribe: res.data.buildList[0].buildBricklayExplain
        })


      }else{
        
      }

        wx.hideLoading();
       
      },
      fail: err => {
        wx.showToast({
          title: '请求失败',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
      }
    })
  },
  // 上传附件
  upload(arr) {
    return new Promise((reslove, reject) => {
      if (arr.length == 0) {
        reslove([])
        return
      }
      wx.showLoading({
        title: '上传中...', //提示的内容
        mask: true //显示透明蒙层 防止触摸穿透
      })
      let finishFilesList = []
      arr.forEach((item, index) => {
        wx.uploadFile({
          url: app.globalData.Interfaceurl + 'saveFileUpload',
          filePath: item,
          name: 'file',
          formData: {
            userId: app.globalData.userId,

          },
          header: {
            "content-type": "multipart/form-data"
          },
          success: function (res) {
            finishFilesList.push(JSON.parse(res.data).fileUrl)
            if (index >= arr.length - 1) {
              setTimeout(() => {
                wx.hideLoading();
                reslove(finishFilesList)
              }, 500)
            }
          },
          fail: err => {
            return err
          }
        })
      })
    })


  },
  saveOrderBuild() {
    let t = this
    wx.showLoading({
      title: '加载中...', //提示的内容
      mask: true //显示透明蒙层 防止触摸穿透
    })
    wx.request({
      url: app.globalData.Interfaceurl + 'saveOrderBuild',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
        userId: app.globalData.userId,
        token: t.data.token,
        orderId: t.data.projectdestil.id,
        buildId: t.data.buildId,
        buildExplain: t.data.projectDescribe,
        node: "施工中",
        atlas: t.data.finishFilesList.join(','),
      },
      success: res => {
        wx.hideLoading();
        if (res.data.result == 0) {
          this.setData({
            issuccessMask: true
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
          t.setData({
            token: app.token()
          })
        }
        t.setData({
          isloadcomplete:false
        })
      },
      fail: err => {
        wx.showToast({
          title: '请求失败',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
      }
    })
  },

  bindUnplone() {
    let t = this
    console.log(9 - t.data.finishFilesList.length)
    wx.chooseMedia({
      count: 9 - t.data.finishFilesList.length,
      mediaType: ['image'],
      success(res) {
        // wx.showLoading({
        //   title: '上传中...', //提示的内容
        //   mask: true //显示透明蒙层 防止触摸穿透
        // })
        let finishFilesList = []
        res.tempFiles.forEach((item, index) => {
          finishFilesList.push(item.tempFilePath)


        })
        t.data.finishFilesList.push(...finishFilesList)
        let finishFilesLists = t.data.finishFilesList
        t.setData({
          finishFilesList: finishFilesLists
        })
      }
    })
  },

  async bindSubmit() {

    if (this.data.finishFilesList.length < 6) {
      wx.showToast({
        title: '上传施工照片不能小于6张',
        duration: 2500, //持续的时间
        icon: 'none', //图标
      })
      return
    }
    if (this.data.projectdestil.buildProofFlag == 0) {
      this.setData({
        finishFilesList: await this.upload(this.data.finishFilesList)
      })
    } else {
      let nofinishFilesList = this.data.finishFilesList.filter(item => item.indexOf(app.globalData.Imageurl) == -1)
      let unfinishFilesList = this.data.finishFilesList.filter(item => item.indexOf(app.globalData.Imageurl) != -1)
      let newfinishFilesList = await this.upload(nofinishFilesList)
      unfinishFilesList.forEach(item => {
        newfinishFilesList.push(item.replace(app.globalData.Imageurl, ""))

      })
      this.setData({
        finishFilesList: newfinishFilesList
      })
    }

    await this.saveOrderBuild()


  },
  HidesuccessMask() {
    this.setData({
      issuccessMask: false
    })
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2]; //prevPage 是获取上一个页面的js里面的pages的所有信息。 -2 是上一个页面，-3是上上个页面以此类推。\\

    prevPage.getOrderBricklayPage()

    wx.navigateBack({
      delta: 1,
    })

  },
  preview(e) {


    wx.previewImage({
      current: e.currentTarget.dataset.url,
      urls: this.data.finishFilesList // 需要预览的图片 http 链接列表
    })
  },
  del(e) {
    let finishFilesList = this.data.finishFilesList
    finishFilesList.splice(e.currentTarget.dataset.index, 1)
    this.setData({
      finishFilesList: finishFilesList
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (!app.globalData.isonLaunch) {
      app.wxlogin()
      .then(res => {
        this.setData({
          projectdestil: JSON.parse(options.projectdestil),
          Imageurl: app.globalData.Imageurl,
          name: app.globalData.name,
          token: app.token()
        })
        if (this.data.projectdestil.buildProofFlag == 1) {
          this.getOrderDetails()
        }
      })
    } else {
      this.setData({
        projectdestil: JSON.parse(options.projectdestil),
        Imageurl: app.globalData.Imageurl,
        name: app.globalData.name,
        token: app.token()
      })
      if (this.data.projectdestil.buildProofFlag == 1) {
        this.getOrderDetails()
      }
    }
    
    if (options.officialAccount) {
      this.setData({
        officialAccount: options.officialAccount,
        ypage: true,
        isloadcomplete:true
      })
      this.getOrderDetails()
    }


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})